 package server.shops;

 import client.MapleCharacter;
import client.MapleClient;
import client.MapleQuestStatus;
import server.quest.MapleQuest;
import tools.packet.PlayerShopPacket;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;










 public final class MapleMiniGame
   extends AbstractPlayerStore
 {
   private static final int slots = 2;
   private final boolean[] exitAfter;
   private final boolean[] ready;
   private final int[] points;
/*  20 */   private final List<Integer> matchcards = new ArrayList<>();
/*  21 */   int loser = 0;
/*  22 */   int turn = 1;
/*  23 */   int piecetype = 0;
/*  24 */   int firstslot = 0;
/*  25 */   int tie = -1;
/*  26 */   private int GameType = 0;
/*  27 */   private int[][] piece = new int[15][15];

   public MapleMiniGame(MapleCharacter owner, int itemId, String description, String pass, int GameType) {
/*  30 */     super(owner, itemId, description, pass, 1);
/*  31 */     this.GameType = GameType;
/*  32 */     this.points = new int[2];
/*  33 */     this.exitAfter = new boolean[2];
/*  34 */     this.ready = new boolean[2];
/*  35 */     reset();
   }

   public void reset() {
/*  39 */     for (int i = 0; i < 2; i++) {
/*  40 */       this.points[i] = 0;
/*  41 */       this.exitAfter[i] = false;
/*  42 */       this.ready[i] = false;
     }
   }

   public int getFirstSlot() {
/*  47 */     return this.firstslot;
   }

   public void setFirstSlot(int type) {
/*  51 */     this.firstslot = type;
   }

   public int getPoints() {
/*  55 */     int ret = 0;
/*  56 */     for (int i = 0; i < 2; i++) {
/*  57 */       ret += this.points[i];
     }
/*  59 */     return ret;
   }

   public void setPoints(int slot) {
/*  63 */     this.points[slot] = this.points[slot] + 1;
/*  64 */     checkWin();
   }

   public void checkWin() {
/*  68 */     if (getPoints() >= getMatchesToWin() && !isOpen()) {
/*  69 */       int x = 0;
/*  70 */       int highest = 0;
/*  71 */       boolean ties = false;
/*  72 */       for (int i = 0; i < 2; i++) {
/*  73 */         if (this.points[i] > highest) {
/*  74 */           x = i;
/*  75 */           highest = this.points[i];
/*  76 */           ties = false;
/*  77 */         } else if (this.points[i] == highest) {
/*  78 */           ties = true;
         }
/*  80 */         this.points[i] = 0;
       }
/*  82 */       broadcastToVisitors(PlayerShopPacket.getMiniGameResult(this, ties ? 1 : 2, x));
/*  83 */       setOpen(true);
/*  84 */       update();
/*  85 */       checkExitAfterGame();
     }
   }

   public int getOwnerPoints(int slot) {
/*  90 */     return this.points[slot];
   }

   public int getPieceType() {
/*  94 */     return this.piecetype;
   }

   public void setPieceType(int type) {
/*  98 */     this.piecetype = type;
   }

   public void setGameType() {
/* 102 */     if (this.GameType == 2) {
/* 103 */       this.matchcards.clear();
/* 104 */       for (int i = 0; i < getMatchesToWin(); i++) {
/* 105 */         this.matchcards.add(Integer.valueOf(i));
/* 106 */         this.matchcards.add(Integer.valueOf(i));
       }
     }
   }

   public void shuffleList() {
/* 112 */     if (this.GameType == 2) {
/* 113 */       Collections.shuffle(this.matchcards);
     } else {
/* 115 */       this.piece = new int[15][15];
     }
   }

   public int getCardId(int slot) {
/* 120 */     return ((Integer)this.matchcards.get(slot - 1)).intValue();
   }

   public int getMatchesToWin() {
/* 124 */     return (getPieceType() == 0) ? 6 : ((getPieceType() == 1) ? 10 : 15);
   }

   public int getLoser() {
/* 128 */     return this.loser;
   }

   public void setLoser(int type) {
/* 132 */     this.loser = type;
   }

   public void send(MapleClient c) {
/* 136 */     if (getMCOwner() == null) {
/* 137 */       closeShop(false, false);
       return;
     }
/* 140 */     c.announce(PlayerShopPacket.openMiniGameBox(c, this));
   }

   public void setReady(int slot) {
/* 144 */     this.ready[slot] = !this.ready[slot];
   }

   public boolean isReady(int slot) {
/* 148 */     return this.ready[slot];
   }

   public void setPiece(int move1, int move2, int type, MapleCharacter chr) {
/* 152 */     if (this.piece[move1][move2] == 0 && !isOpen()) {
/* 153 */       this.piece[move1][move2] = type;
/* 154 */       broadcastToVisitors(PlayerShopPacket.getMiniGameMoveOmok(move1, move2, type));
/* 155 */       boolean found = false;
/* 156 */       for (int y = 0; y < 15; y++) {
/* 157 */         for (int x = 0; x < 15; x++) {
/* 158 */           if (!found && searchCombo(x, y, type)) {
/* 159 */             broadcastToVisitors(PlayerShopPacket.getMiniGameResult(this, 2, getVisitorSlot(chr)));
/* 160 */             setOpen(true);
/* 161 */             update();
/* 162 */             checkExitAfterGame();
/* 163 */             found = true;
           }
         }
       }
/* 167 */       nextLoser();
     }
   }

   public void nextLoser() {
/* 172 */     this.loser++;
/* 173 */     if (this.loser > 1) {
/* 174 */       this.loser = 0;
     }
   }

   public void exit(MapleCharacter player) {
/* 179 */     if (player == null) {
       return;
     }
/* 182 */     player.setPlayerShop(null);
/* 183 */     if (isOwner(player)) {
/* 184 */       update();
/* 185 */       removeAllVisitors(3, 1);
     } else {
/* 187 */       removeVisitor(player);
     }
   }

   public boolean isExitAfter(MapleCharacter player) {
/* 192 */     return (getVisitorSlot(player) > -1 && this.exitAfter[getVisitorSlot(player)]);
   }

   public void setExitAfter(MapleCharacter player) {
/* 196 */     if (getVisitorSlot(player) > -1) {
/* 197 */       this.exitAfter[getVisitorSlot(player)] = !this.exitAfter[getVisitorSlot(player)];
     }
   }

   public void checkExitAfterGame() {
/* 202 */     for (int i = 0; i < 2; i++) {
/* 203 */       if (this.exitAfter[i]) {
/* 204 */         this.exitAfter[i] = false;
/* 205 */         exit((i == 0) ? getMCOwner() : this.chrs[i - 1].get());
       }
     }
   }

   public boolean searchCombo(int x, int y, int type) {
/* 211 */     boolean ret = false;
/* 212 */     if (!ret && x < 11) {
/* 213 */       ret = true;
/* 214 */       for (int i = 0; i < 5; i++) {
/* 215 */         if (this.piece[x + i][y] != type) {
/* 216 */           ret = false;
           break;
         }
       }
     }
/* 221 */     if (!ret && y < 11) {
/* 222 */       ret = true;
/* 223 */       for (int i = 0; i < 5; i++) {
/* 224 */         if (this.piece[x][y + i] != type) {
/* 225 */           ret = false;
           break;
         }
       }
     }
/* 230 */     if (!ret && x < 11 && y < 11) {
/* 231 */       ret = true;
/* 232 */       for (int i = 0; i < 5; i++) {
/* 233 */         if (this.piece[x + i][y + i] != type) {
/* 234 */           ret = false;
           break;
         }
       }
     }
/* 239 */     if (!ret && x > 3 && y < 11) {
/* 240 */       ret = true;
/* 241 */       for (int i = 0; i < 5; i++) {
/* 242 */         if (this.piece[x - i][y + i] != type) {
/* 243 */           ret = false;
           break;
         }
       }
     }
/* 248 */     return ret;
   }


   public int getScore(MapleCharacter chr) {
/* 253 */     int score = 2000;
/* 254 */     int wins = getWins(chr);
/* 255 */     int ties = getTies(chr);
/* 256 */     int losses = getLosses(chr);
/* 257 */     if (wins + ties + losses > 0) {
/* 258 */       score += wins * 2;
/* 259 */       score += ties;
/* 260 */       score -= losses * 2;
     }
/* 262 */     return score;
   }


@Override
public byte getShopType() {
    return GameType == 1 ? IMaplePlayerShop.OMOK : IMaplePlayerShop.MATCH_CARD;
}





   public int getWins(MapleCharacter chr) {
/* 275 */     return Integer.parseInt(getData(chr).split(",")[2]);
   }

   public int getTies(MapleCharacter chr) {
/* 279 */     return Integer.parseInt(getData(chr).split(",")[1]);
   }

   public int getLosses(MapleCharacter chr) {
/* 283 */     return Integer.parseInt(getData(chr).split(",")[0]);
   }

   public void setPoints(int i, int type) {
     MapleCharacter z;
/* 288 */     if (i == 0) {
/* 289 */       z = getMCOwner();
     } else {
/* 291 */       z = getVisitor(i - 1);
     }
/* 293 */     if (z != null) {
/* 294 */       String[] data = getData(z).split(",");
/* 295 */       data[type] = String.valueOf(Integer.parseInt(data[type]) + 1);
/* 296 */       StringBuilder newData = new StringBuilder();
/* 297 */       for (String aData : data) {
/* 298 */         newData.append(aData);
/* 299 */         newData.append(",");
       }
/* 301 */       String newDat = newData.toString();
/* 302 */       z.getQuestNAdd(MapleQuest.getInstance((this.GameType == 1) ? 122200 : 122210)).setCustomData(newDat.substring(0, newDat.length() - 1));
     }
   }
   public String getData(MapleCharacter chr) {
     MapleQuestStatus record;
/* 307 */     MapleQuest quest = MapleQuest.getInstance((this.GameType == 1) ? 122200 : 122210);

/* 309 */     if (chr.getQuestNoAdd(quest) == null) {
/* 310 */       record = chr.getQuestNAdd(quest);
/* 311 */       record.setCustomData("0,0,0");
     } else {
/* 313 */       record = chr.getQuestNoAdd(quest);
/* 314 */       if (record.getCustomData() == null || record.getCustomData().length() < 5 || !record.getCustomData().contains(",")) {
/* 315 */         record.setCustomData("0,0,0");
       }
     }
/* 318 */     return record.getCustomData();
   }

   public int getRequestedTie() {
/* 322 */     return this.tie;
   }

   public void setRequestedTie(int t) {
/* 326 */     this.tie = t;
   }

   public int getTurn() {
/* 330 */     return this.turn;
   }

   public void setTurn(int t) {
/* 334 */     this.turn = t;
   }


   public void closeShop(boolean s, boolean z) {
/* 339 */     removeAllVisitors(3, 1);
/* 340 */     if (getMCOwner() != null) {
/* 341 */       getMCOwner().setPlayerShop(null);
     }
/* 343 */     update();
/* 344 */     getMap().removeMapObject(this);
   }

   public void buy(MapleClient c, int z, short i) {}
 }


/* Location:              C:\PGX\Server.jar!\server\shops\MapleMiniGame.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */